import { load, getTimestamp } from '@/utils'

const module = {
  namespaced: true,
  state: {
    cinemaList: [],
    ticketFlag: 1
  },
  mutations: {
    setCinemaList (state, cinemaList) {
      state.cinemaList = cinemaList
    },
    setTicketFlag (state, ticketFlag) {
      state.ticketFlag = ticketFlag
    }
  },
  actions: {
    // getCinemaList(store, obj) { // 以下用到了 解构赋值
    getCinemaList (store, { cityId, ticketFlag = 1 }) {
      store.commit('setTicketFlag', ticketFlag)
      return load({
        url: `/gateway?cityId=${cityId}&ticketFlag=${ticketFlag}&k=${getTimestamp()}`,
        headers: { 'X-Host': 'mall.film-ticket.cinema.list' }
      }).then(res => {
        store.commit('setCinemaList', res.data.data.cinemas)
      }).catch((err) => {
        console.warn(err)
        store.commit('setCinemaList', [])
      })
    }
  }
}

export default module
